<template>
  <div>
    <!-- 搜索 -->
    <el-input class="input" placeholder="请输入名字" v-model="name"> </el-input>
    <el-button type="success" @click="sousuo">搜索</el-button>

    <el-table :data="resList" border style="width: 100%">
      <el-table-column prop="uid" label="uid" width="180"> </el-table-column>
      <el-table-column prop="nickname" label="姓名" width="180">
      </el-table-column>
      <el-table-column prop="mobile" label="电话号"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="remove(scope.$index)">
            <!-- scope.$index代表这条数据的下标 -->
            移除
          </el-button>
          <el-button type="text" size="small" @click="edit(scope.row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="list.length"
      :page-size="pageSize"
      :current-page="pageNum"
      @current-change="changepage"
    >
    </el-pagination>

    <!-- 修改框 -->
    <el-dialog title="修改信息" :visible.sync="editFlag">
      <el-form :model="student">
        <el-form-item label="姓名">
          <el-input v-model="student.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话号码">
          <el-input v-model="student.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: [],
      copylist: [],
      // 分页器
      pageNum: 1,
      pageSize: 5,
      // 控制编辑的对话框隐藏和显示
      editFlag: false,
      student: {
        uid: "",
        name: "",
        phone: "",
        status: "",
      },
      name: "",
    };
  },
  computed: {
    resList() {
      return this.list.slice(
        this.pageSize * (this.pageNum - 1),
        this.pageSize * this.pageNum
      );
    },
  },
  mounted() {
    this.$axios.get("/list.json").then((res) => {
      console.log(res);
      this.list = res.data;
      this.copylist = res.data;
    });
  },
  methods: {
    // 分液器
    changepage(val) {
      this.pageNum = val;
    },
    // 删除
    remove(index) {
      this.list.splice(index, 1);
    },
    //  点击编辑按钮时，弹出对话框，在对话框里显示要编辑的内容
    edit(val) {
      this.editFlag = true;
      this.student.uid = val.uid;
      this.student.name = val.nickname;
      this.student.phone = val.mobile;
      this.student.status = val.status;
    },

    // 点击编辑对话确定按钮时，根据uid修改list里相应的内容
    saveEdit() {
      this.editFlag = false;
      this.list.forEach((item) => {
        if (item.uid == this.student.uid) {
          item.nickname = this.student.name;
          item.mobile = this.student.phone;
          item.status = this.student.status;
        }
      });
    },
    // 搜索
    sousuo() {
        this.list=this.copylist.filter((item)=>{
            return item.nickname.includes(this.name);
        })

    },
  },
};
</script>
<style >
.input {
  width: 200px;
}
</style>